/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Ventas;

import Atxy2k.CustomTextField.RestrictedTextField;
import java.util.ArrayList;
import javax.swing.JDialog;
import Controlador.Bean.Ventas.Cliente;
import Controlador.Bean.Ventas.Departamento;
import Controlador.Bean.Ventas.Provincia;
import Controlador.Bean.Ventas.Distrito;
import Controlador.Service.Ventas.GestionVentas;
import javax.swing.JOptionPane;
import lib.Validaciones;
/**
 *
 * @author Luis Enrique
 */
public class ClienteAgregarPanel extends javax.swing.JPanel {

    private JDialog parent;
    ArrayList<Departamento> departamentos = GestionVentas.getInstance().consultarDepartamento();
    ArrayList<Provincia> provincias=null;
    ArrayList<Distrito> distritos=null;
    private ClienteMainPanel RefClientePanel;
    private ClienteBuscarPanel RefClientePanel2;
     
    public void setParent(JDialog parent){
        this.parent = parent;
    }
    
    public void setRefClientePanel(ClienteMainPanel panel){
        RefClientePanel = panel;
    }
    
    public void setRefClientePanel2(ClienteBuscarPanel panel){
        RefClientePanel2 = panel;
    }
    
    public ClienteAgregarPanel() {
        initComponents();
        btnGpTipoPersona.add(rbPJuridica);
        btnGpTipoPersona.add(rbPNatural);
        btnGpTipoDocumento.add(rbRUC);
        btnGpTipoDocumento.add(rbDNI);
        
        //CAMPOS: RAZON SOCIAL Y NOMBRE
        txtRazonSocial.setEnabled(false);
        txtApePaterno.setEnabled(false);
        txtApeMaterno.setEnabled(false);
        txtNombres.setEnabled(false);
        rbRUC.setEnabled(false);
        rbDNI.setEnabled(false);
        txtNumDoc.setEnabled(false);
        
        //CMB DEPARTAMENTO
        cmbDepartamento.addItem("Seleccionar");      
        for(int i=0; i<departamentos.size(); i++){
             cmbDepartamento.addItem(departamentos.get(i).getNombre());
        }
        cmbProvincia.setEnabled(false);
        cmbDistrito.setEnabled(false);
        
        RestrictedTextField restricted = new RestrictedTextField(txtRazonSocial);
        restricted.setLimit(50);
        
        restricted = new RestrictedTextField(txtApePaterno);
        restricted.setLimit(50);
        restricted.setOnlyText(true);
                
        restricted = new RestrictedTextField(txtApeMaterno);
        restricted.setLimit(50);
        restricted.setOnlyText(true);
        
        restricted = new RestrictedTextField(txtNombres);
        restricted.setLimit(50);
        restricted.setOnlyText(true);
        
        restricted = new RestrictedTextField(txtNumDoc);
        restricted.setLimit(11);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtDireccion);
        restricted.setLimit(50);
        
        restricted = new RestrictedTextField(txtReferencia);
        restricted.setLimit(50);
        
        restricted = new RestrictedTextField(txtTelefono);
        restricted.setLimit(9);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtReferencia);
        restricted.setLimit(50);
        
        restricted = new RestrictedTextField(txtEMail);
        restricted.setLimit(50);
    }
    
    public void refreshDetails(){
        if(rbPJuridica.isSelected()){
            txtRazonSocial.setText("");
            txtRazonSocial.setEnabled(true);
            txtApePaterno.setText("");
            txtApePaterno.setEnabled(false);
            txtApeMaterno.setText("");
            txtApeMaterno.setEnabled(false);
            txtNombres.setText("");
            txtNombres.setEnabled(false);
            rbRUC.setSelected(true);
            rbRUC.setEnabled(false);
            rbDNI.setEnabled(false);
            txtNumDoc.setText("");
            txtNumDoc.setEnabled(true);
        }
        else{
            txtRazonSocial.setText("");
            txtRazonSocial.setEnabled(false);
            txtApePaterno.setText("");
            txtApePaterno.setEnabled(true);
            txtApeMaterno.setText("");
            txtApeMaterno.setEnabled(true);
            txtNombres.setText("");
            txtNombres.setEnabled(true);            
            btnGpTipoDocumento.clearSelection();
            rbRUC.setEnabled(true);
            rbDNI.setEnabled(true);
            txtNumDoc.setText("");
            txtNumDoc.setEnabled(true);
        }    
    }
    
    public boolean valido(){
        boolean b = true;
        if(!rbPJuridica.isSelected() && !rbPNatural.isSelected()){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Tipo de Persona", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(rbPJuridica.isSelected() && txtRazonSocial.getText().isEmpty()){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Razón Social", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(rbPNatural.isSelected() && (txtApePaterno.getText().isEmpty() || !Validaciones.validarNombre(txtApePaterno.getText()))){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Ape. Paterno", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(rbPNatural.isSelected() && (txtApeMaterno.getText().isEmpty() || !Validaciones.validarNombre(txtApeMaterno.getText()))){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Ape. Materno", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(rbPNatural.isSelected() && (txtNombres.getText().isEmpty() || !Validaciones.validarNombre(txtApePaterno.getText()))){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Nombres", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(rbRUC.isSelected())
            if(txtNumDoc.getText().isEmpty() || txtNumDoc.getText().length()!=11){
                b = false;
                JOptionPane.showMessageDialog(this, "Error en Num. de Documento RUC", "Error en los datos", JOptionPane.OK_OPTION);
            }           
        if(rbDNI.isSelected())
            if(txtNumDoc.getText().isEmpty() || txtNumDoc.getText().length()!=8){
                b = false;
                JOptionPane.showMessageDialog(this, "Error en Num. de Documento DNI", "Error en los datos", JOptionPane.OK_OPTION);
            }
        if(!rbDNI.isSelected() && !rbRUC.isSelected()){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Tipo de Documento", "Error en los datos", JOptionPane.OK_OPTION);
        }                           
        if(cmbDepartamento.getSelectedIndex()<1){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Departamento", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(cmbProvincia.getSelectedIndex()<1){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Provincia", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(cmbDistrito.getSelectedIndex()<1){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Distrito", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(txtDireccion.getText().isEmpty()){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Dirección", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(txtTelefono.getText().isEmpty() || (txtTelefono.getText().length()!=7 && txtTelefono.getText().length()!=9)){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Teléfono", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(txtEMail.getText().isEmpty() || !Validaciones.validarEmail(txtEMail.getText())){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en E-mail", "Error en los datos", JOptionPane.OK_OPTION);
        }
        return b;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGpTipoPersona = new javax.swing.ButtonGroup();
        btnGpTipoDocumento = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        rbPJuridica = new javax.swing.JRadioButton();
        rbPNatural = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        txtRazonSocial = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtApePaterno = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtApeMaterno = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtNombres = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        rbRUC = new javax.swing.JRadioButton();
        rbDNI = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        txtNumDoc = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        cmbDepartamento = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        cmbProvincia = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        cmbDistrito = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtReferencia = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtEMail = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Generales"));

        jLabel1.setText("Tipo de Persona");

        rbPJuridica.setText("P. Jurídica");
        rbPJuridica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPJuridicaActionPerformed(evt);
            }
        });

        rbPNatural.setText("P. Natural");
        rbPNatural.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPNaturalActionPerformed(evt);
            }
        });

        jLabel2.setText("Razón Social");

        jLabel3.setText("Apellido Paterno");

        jLabel4.setText("Apellido Materno");

        jLabel5.setText("Nombres");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(41, 41, 41)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rbPJuridica)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                        .addComponent(rbPNatural))
                    .addComponent(txtNombres)
                    .addComponent(txtApeMaterno)
                    .addComponent(txtApePaterno)
                    .addComponent(txtRazonSocial))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbPJuridica)
                    .addComponent(jLabel1)
                    .addComponent(rbPNatural))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRazonSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtApePaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtApeMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de Documento"));

        jLabel6.setText("Tipo de Documento");

        rbRUC.setText("R.U.C.");

        rbDNI.setText("D.N.I.");

        jLabel7.setText("Número de Doc.");

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel15.setText("R.U.C. (11 dígitos) y D.N.I. (8 dígitos)");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 29, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addComponent(rbRUC)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(rbDNI))
                            .addComponent(txtNumDoc, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel15)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbRUC)
                    .addComponent(jLabel6)
                    .addComponent(rbDNI))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel15))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de Contacto"));
        jPanel2.setMaximumSize(new java.awt.Dimension(353, 256));
        jPanel2.setMinimumSize(new java.awt.Dimension(353, 256));

        jLabel8.setText("Departamento");

        cmbDepartamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbDepartamentoActionPerformed(evt);
            }
        });

        jLabel9.setText("Provincia");

        cmbProvincia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbProvinciaActionPerformed(evt);
            }
        });

        jLabel10.setText("Distrito");

        jLabel11.setText("Dirección");

        jLabel14.setText("Referencia");

        jLabel12.setText("Teléfono");

        jLabel13.setText("E-Mail");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel9))
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(jLabel14))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cmbDepartamento, javax.swing.GroupLayout.Alignment.TRAILING, 0, 200, Short.MAX_VALUE)
                                    .addComponent(cmbProvincia, javax.swing.GroupLayout.Alignment.TRAILING, 0, 200, Short.MAX_VALUE)
                                    .addComponent(cmbDistrito, javax.swing.GroupLayout.Alignment.TRAILING, 0, 200, Short.MAX_VALUE)
                                    .addComponent(txtDireccion, javax.swing.GroupLayout.Alignment.TRAILING)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(52, 52, 52)
                                .addComponent(txtReferencia))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(jLabel13))
                        .addGap(79, 79, 79)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTelefono)
                            .addComponent(txtEMail))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(cmbDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbProvincia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbDistrito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(txtReferencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnGuardar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnCancelar)
                            .addComponent(btnGuardar)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void rbPNaturalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPNaturalActionPerformed
        refreshDetails();
    }//GEN-LAST:event_rbPNaturalActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        if(valido()){
            String razonSocial = (txtRazonSocial.getText().equals(""))?null:txtRazonSocial.getText();
            String apePaterno = (txtApePaterno.getText().equals(""))?null:txtApePaterno.getText();
            String apeMaterno = (txtApeMaterno.getText().equals(""))?null:txtApeMaterno.getText();
            String nombres = (txtNombres.getText().equals(""))?null:txtNombres.getText();
            String referencia = (txtReferencia.getText().equals(""))?null:txtReferencia.getText();

            if(rbPJuridica.isSelected()){
                Cliente cliente = new Cliente(null,"TPJ",razonSocial,null,null,null,"ruc",txtNumDoc.getText(),txtDireccion.getText(),referencia,distritos.get(cmbDistrito.getSelectedIndex()-1).getIdDistrito(),txtTelefono.getText(),txtEMail.getText(),"Activo");
                Controlador.Service.Ventas.GestionVentas.agregarCliente(cliente);
            }
            else{
                Cliente cliente = new Cliente(null,"TPN",null,apePaterno,apeMaterno,nombres,"dni",txtNumDoc.getText(),txtDireccion.getText(),referencia,distritos.get(cmbDistrito.getSelectedIndex()-1).getIdDistrito(),txtTelefono.getText(),txtEMail.getText(),"Activo");
                Controlador.Service.Ventas.GestionVentas.agregarCliente(cliente);
            }
            JOptionPane.showMessageDialog(this,"Se registró correctamente el cliente");
            if(RefClientePanel!=null)
                RefClientePanel.refreshItemsTable();
            else
                RefClientePanel2.refreshItemsTable();
            parent.dispose();
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        parent.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void rbPJuridicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPJuridicaActionPerformed
        refreshDetails();
    }//GEN-LAST:event_rbPJuridicaActionPerformed

    private void cmbDepartamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbDepartamentoActionPerformed
        if(cmbDepartamento.getSelectedIndex()>0){
            cmbProvincia.removeAllItems();
            cmbDistrito.removeAllItems();
            cmbProvincia.setEnabled(true);
            cmbDistrito.setEnabled(false);
            cmbProvincia.addItem("Seleccionar");
            provincias=GestionVentas.getInstance().consultarProvincia(departamentos.get(cmbDepartamento.getSelectedIndex()-1).getIdDepartamento());
            for(int i=0; i<provincias.size(); i++){
                cmbProvincia.addItem(provincias.get(i).getNombre());
            }
        }
        else{
            cmbProvincia.setEnabled(false);
            cmbDistrito.setEnabled(false);
            cmbProvincia.removeAllItems();
            cmbDistrito.removeAllItems();    
        }
    }//GEN-LAST:event_cmbDepartamentoActionPerformed

    private void cmbProvinciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbProvinciaActionPerformed
        if(cmbProvincia.getSelectedIndex()>0){
            cmbDistrito.removeAllItems();
            cmbDistrito.setEnabled(true);
            cmbDistrito.addItem("Seleccionar");
            distritos=GestionVentas.getInstance().consultarDistrito(provincias.get(cmbProvincia.getSelectedIndex()-1).getIdProvincia());
            for(int i=0; i<distritos.size(); i++){
             cmbDistrito.addItem(distritos.get(i).getNombre());
            }
        }
        else{
            cmbDistrito.setEnabled(false);
            cmbDistrito.removeAllItems(); 
        }
    }//GEN-LAST:event_cmbProvinciaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.ButtonGroup btnGpTipoDocumento;
    private javax.swing.ButtonGroup btnGpTipoPersona;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cmbDepartamento;
    private javax.swing.JComboBox cmbDistrito;
    private javax.swing.JComboBox cmbProvincia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton rbDNI;
    private javax.swing.JRadioButton rbPJuridica;
    private javax.swing.JRadioButton rbPNatural;
    private javax.swing.JRadioButton rbRUC;
    private javax.swing.JTextField txtApeMaterno;
    private javax.swing.JTextField txtApePaterno;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtEMail;
    private javax.swing.JTextField txtNombres;
    private javax.swing.JTextField txtNumDoc;
    private javax.swing.JTextField txtRazonSocial;
    private javax.swing.JTextField txtReferencia;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables
}
